package com.cms.parent;

import com.auxiliary.Util;
import com.doman.Parent;
import com.resultmsg.BaseEnums;
import com.resultmsg.Result;
import com.resultmsg.Results;
import com.server.ParentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/educloud")
@RestController
public class ParentController {

    @Autowired
    private ParentMapper mapper;


    /**
     *获取学生家长列表
     * @param bindcode 绑定码
     * @return
     */
    @RequestMapping(value = "/parents/{bindcode}", method = RequestMethod.GET)
    public Result getParentByBindCode(@PathVariable("bindcode") String bindcode) {
        try {
            List<Parent> list = mapper.selectByBindCode(bindcode);
            return Results.successWithData(list,BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
        } catch (Exception e) {
            return Results.failureWithData(e, BaseEnums.OPERATION_FAILURE.code(), BaseEnums.OPERATION_FAILURE.desc());

        }

    }

    /**
     * 创建家长角色
     *
     * @param parent
     * @return
     */
    @RequestMapping(value = "/parents", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result addParent(@RequestBody Parent parent) {
        try{
            parent.setCreateTime(Util.getCurrentTime());
            parent.setUpdateTime(Util.getCurrentTime());
            int res = mapper.insertSelective(parent);
            if (res > 0) {
                return Results.success(BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
            } else {
                return Results.failure(BaseEnums.OPERATION_FAILURE.code(), BaseEnums.OPERATION_FAILURE.desc());
            }
        } catch (Exception e) {
            return Results.failureWithData(e, BaseEnums.OPERATION_FAILURE.code(), BaseEnums.OPERATION_FAILURE.desc());
        }

    }


    /**
     * 更新家长角色
     *
     * @param parent
     * @return
     */
    @RequestMapping(value = "/parents", method = RequestMethod.PUT, produces = {"application/json;charset=UTF-8"})
    public Result updateParent(@RequestBody Parent parent) {
        try{
            parent.setUpdateTime(Util.getCurrentTime());
            int res = mapper.updateByPrimaryKeySelective(parent);
            if (res > 0) {
                return Results.success(BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
            } else {
                return Results.failure(BaseEnums.OPERATION_FAILURE.code(), BaseEnums.OPERATION_FAILURE.desc());
            }
        } catch (Exception e) {
            return Results.failureWithData(e, BaseEnums.OPERATION_FAILURE.code(), BaseEnums.OPERATION_FAILURE.desc());
        }

    }

    /**
     * 删除家长角色(未完成)
     *
     * @param parent
     * @return
     */
    @RequestMapping(value = "/parents", method = RequestMethod.DELETE, produces = {"application/json;charset=UTF-8"})
    public Result delParent(@RequestBody Parent parent) {
        try {

//            parent.setCreateTime(Util.getCurrentTime());
//            parent.setUpdateTime(Util.getCurrentTime());

            int res = mapper.deleteByPrimaryKey(parent.getId());
            if (res > 0) {
                return Results.success(BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
            } else {
                return Results.failure(BaseEnums.OPERATION_FAILURE.code(), BaseEnums.OPERATION_FAILURE.desc());
            }
        } catch (Exception e) {
            return Results.failureWithData(e, BaseEnums.OPERATION_FAILURE.code(), BaseEnums.OPERATION_FAILURE.desc());
        }

    }





}
